<?php
namespace backend\controllers;

use Yii;
use backend\models\LoginForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\base\BaseBackendController;

use yii\helpers\Url;
/**
 * Site controller
 */
class IndexController extends BaseBackendController
{
	public $layout = false;
	
	
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                //'only' => ['special-callback'],
                'rules' => [
                    [
                        'actions' => ['signup', 'index2', 'login'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout', 'index', 'index2', 'get-shortcut-menu'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
					
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    //'logout' => ['post'],
                ],
            ],
        ];
    }



    public function actionIndex()
    {
		//exit(Url::to(['user/bbb', 'id' => $this->id, 'aa'=>1111]));
		$data['menu']['parent'] = Yii::$app->FileCache ->get('backend_menu_parent');
		$data['menu']['data'] = Yii::$app->FileCache ->get('backend_menu');
		//print_r($data['menu']);
        return $this->render('index', $data);
    }
	
	public function actionShortcutMenu(){
		
		$this->render('shortcutMenu');
		
	}
	
	public function actionUpdateShortcutMenu($menuid, $shortcut = 0){
		$sql = "update {{%backend_menu}} set shortcut = $shortcut where menuid = $menuid";
		$return = Yii::$app->db->createCommand($sql)->execute();
		Yii::app()->runController('mobo/menu/cache');
	}

	
	public function actionGetShortcutMenu($return = false){
		$menu = array();
		$data = Yii::$app->FileCache ->get('backend_menu');
		foreach($data as $k=>$v){
			if ($v['shortcut']) $menu[] = $v;
		}
		if($return){
			return $menu;
		}else{
		    echo json_encode($menu);
		}
		
	}
	
	
	public function actionIndex2()
    {
		
        return $this->render('index');
    }
	
	public function menuCreateUrl($route){
		
		$url = Yii::$app->urlManager->createAbsoluteUrl($route);
		if (strpos($url, '@submodule')){
			$url = str_replace(['@submodule', 'backend'], ['', 'submodule'], $url);
		}
		return $url;
	}


}
